package com.doit.day03;

import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.*;

import java.util.*;

public class CompartorDemo {
    public static void main(String[] args) {
        ArrayList<Order> list = new ArrayList<>();
        list.add(new Order("u001","o001","g001","c002",998.99,1));
        list.add(new Order("u002","o001","g003","c002",98.99,2));
        list.add(new Order("u001","o003","g003","c003",8.99,1));
        list.add(new Order("u003","o002","g002","c003",9.99,22));
        list.add(new Order("u001","o003","g002","c004",99.99,3));
        list.add(new Order("u004","o003","g002","c001",118.99,4));
        list.add(new Order("u002","o002","g001","c001",228.99,8));

        //是怎么比较的呢？？
        //第二个参数传的是比较器
/*        Collections.sort(list, new Comparator<Order>() {
            @Override
            public int compare(Order o1, Order o2) {
                return Double.compare(o1.getPrice()*o1.getGoods_num(),o2.getPrice()*o2.getGoods_num());
            }
        });

        for (Order order : list) {
            System.out.println(order);
        }*/
        // 需求3：统计每一种品类下的订单总额
        //整一个hashmap，然后将品类作为key ，订单总额作为value
/*        HashMap<String, Double> map = new HashMap<>();
        for (Order order : list) {
            Double value = map.getOrDefault(order.getCategory_id(), 0.0);
            map.put(order.getCategory_id(),value+ order.getPrice()*order.getGoods_num());
        }
        Set<String> set = map.keySet();
        for (String key : set) {
            System.out.println(key+","+map.get(key));
        }*/
        //需求5：找出每个用户金额最大的哪个订单
        //hashmap  把value改成ArrayList，ArrayList放得是order==》针对这个arrayList进行排序(降序排列) 取第一个
        HashMap<String, ArrayList<Order>> map = new HashMap<>();

        for (Order order : list) {
            ArrayList<Order> orders1 = map.get(order.getUser_id());
            //如果原先的map中没有这个key，get出来就是一个null
            if (orders1 == null){
                orders1 = new ArrayList<>();
                orders1.add(order);
            }else {
                //如果这个key本身就存在
                orders1.add(order);
            }
            map.put(order.getUser_id(),orders1);
        }
        Set<String> keys = map.keySet();

        for (String key : keys) {
            ArrayList<Order> orders = map.get(key);
            Collections.sort(orders);
            //拍完序后金额最大的order
            Order order = orders.get(0);
            System.out.println(key+","+order);
        }


    }
}


/**
 * 属性：user_id , order_id，goods_id，category_id，price，goods_num
 * 需求1：对7个订单的金额大小的升序排列
 * 需求3：统计每一种品类下的订单总额
 * 需求5：找出每个用户金额最大的哪个订单
 */
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@ToString
class Order implements Comparable<Order>{
    private String user_id;
    private String order_id;
    private String goods_id;
    private String category_id;
    private Double price;
    private Integer goods_num;

    @Override
    public int compareTo(Order o) {
        return -Double.compare(this.getGoods_num()*this.getPrice(),o.getGoods_num()*o.getPrice());
    }
}
